package com.sumainfor.pc.epc.controller.buyer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.pc.epc.service.BuyerManageService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author PuaChen
 * @Create 2018-11-08 14:52
 */
@RestController
@RequestMapping("/buyer/manage")
public class BuyerManageController {

    @Autowired
    private BuyerManageService bms;

    /**
     * 获取 装修采购单 或者 备件采购单 列表
     *
     * @param params
     * @param page
     * @return
     */
    @RequestMapping("/getBuyerListForType")
    public JsonResult getBuyerListForType(@RequestParam Map<String, Object> params, Page page) {
        String[] field = {"type", "state"};
        if (!FieldCheckUtil.isMapFieldNotNull(params, field)) {
            return JsonResult.error("参数错误");
        }
        return bms.getBuyerList(params, page);
    }

    /**
     * 获取装修采购中的详细信息中的商品的列表 Tab采购单
     *
     * @param buyId
     * @return
     */
    @RequestMapping("/getBuyerDetailAsZXDone")
    public JsonResult getBuyerDetailAsZXDone(String buyId) {
        if (StringUtils.isBlank(buyId)) {
            return JsonResult.error("参数错误");
        }
        return bms.getBuyerDetailAsZXDone(buyId);
    }

    /**
     * 获取装修采购中的详细信息中的商品的列表 Tab出库清单
     *
     * @param buyerId
     * @return
     */
    @RequestMapping("/getOutPutStockShopList")
    public JsonResult getOutPutStockShopList(String buyerId) {
        if (StringUtils.isBlank(buyerId)) {
            return JsonResult.errorForEmpty();
        }
        return bms.getOutPutStockShopList(buyerId);
    }

    /**
     * 创建采购单以及出库清单 第一步
     *
     * @param buyerId
     * @return
     */
    @PostMapping("/createBuyerAndOutForOrder")
    public JsonResult createBuyerAndOutForOrder(String buyerId) {
        if (StringUtils.isBlank(buyerId)) {
            return JsonResult.errorForEmpty();
        }
        return bms.createBuyerAndOutForOrder(buyerId);
    }

    /**
     * 查看采购清单 Tab2采购清单
     *
     * @param buyerId
     * @return
     */
    @RequestMapping("/getNotBuyerOrderInfo")
    public JsonResult getNotBuyerOrderInfo(String buyerId) {
        if (StringUtils.isBlank(buyerId)) {
            return JsonResult.errorForEmpty();
        }
        return bms.getNotBuyerOrderInfo(buyerId);
    }

    /**
     * 获取装修采购中的详细信息中的商品的列表 Tab物品清单
     *
     * @param buyId
     * @return
     */
    @RequestMapping("/getBuyerDetailAsGoodsList")
    public JsonResult getBuyerDetailAsGoodsList(String buyId) {
        if (StringUtils.isBlank(buyId)) {
            return JsonResult.error("参数错误");
        }
        return bms.getBuyerDetailAsGoodsList(buyId);
    }

    /**
     * 获取商品品牌筛选的列表
     *
     * @param speId
     * @return
     */
    @RequestMapping("/getBrandList")
    public JsonResult getBrandList(String speId) {
        if (StringUtils.isBlank(speId)) {
            return JsonResult.error("参数错误");
        }
        return bms.getBrandList(speId);
    }

    /**
     * 通过规格的ids 查询所有的型号信息
     *
     * @param ids
     * @return
     */
    @RequestMapping("/getTypeByComIds")
    public JsonResult getTypeByComIds(@RequestParam List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return JsonResult.error("参数错误");
        }
        return bms.getTypeByComIds(ids);
    }

    /**
     * 查看商品所属房源的详情
     *
     * @param speId
     * @param buyId
     * @return
     */
    @RequestMapping("/getShopRelateHousingForZx")
    public JsonResult getShopRelateHousingForZx(String speId, String buyId) {
        if (StringUtils.isBlank(speId) || StringUtils.isBlank(buyId)) {
            return JsonResult.error("参数错误");
        }
        return bms.getShopRelateHousingForZx(speId, buyId);
    }


    /**
     * 发起装修采购单 保存或者创建
     *
     * @param params
     * @return
     */
    @PostMapping("/createBuyerOrderOrSave")
    public JsonResult createBuyerOrder(@RequestBody Map<String, Object> params) {
        String[] filed = {"type", "BuyId"};
        if (!FieldCheckUtil.isMapFieldNotNull(params, filed)) {
            return JsonResult.error("参数错误");
        }
        return bms.createBuyerOrder(params);
    }


    /**
     * 更新采购单详细表的 状态为采购完成
     *
     * @param params
     * @return
     */
    @PostMapping("/setBuyerInfoStateForDone")
    public JsonResult setBuyerInfoStateForDone(@RequestBody Map<String, Object> params) {
        if (MapUtils.isEmpty(params) || !FieldCheckUtil.isMapFieldNotNull(params, "ids", "buyId")) {
            return JsonResult.errorForEmpty();
        }
        List<Integer> ids = (List<Integer>) params.get("ids");
        String buyId = (String) params.get("buyId");
        if (ids == null || ids.isEmpty() || StringUtils.isBlank(buyId)) {
            return JsonResult.error("参数错误");
        }
        return bms.setBuyerInfoStateForDone(ids, buyId);
    }

    /**
     * 装修采购的 出库商品的操作
     *
     * @param params {
     *               "list":[
     *               {
     *               "id":1,   //id
     *               "count":5  // 出库的数量,
     *               "ComSpeMoId":"",   //型号的编号
     *               "SupId":""   //供应商的编号,
     *               "ComId":"",  //商品编号
     *               "ComSpeId":""  //规格编号
     *               }
     *               ],
     *               "buyId":2018  采购单主编号
     *               }
     * @return
     */
    @PostMapping("/OutBuyerZxAction")
    public JsonResult buyerOutAction(@RequestBody Map<String, Object> params) {
        if (MapUtils.isEmpty(params) || !FieldCheckUtil.isMapFieldNotNull(params, "list", "buyId")) {
            return JsonResult.errorForEmpty();
        }
        List<Map<String, Object>> data = (List<Map<String, Object>>) params.get("list");
        String buyId = (String) params.get("buyId");
        if (data.isEmpty()) {
            return JsonResult.errorForEmpty();
        }
        return bms.buyerOutAction(data, buyId);
    }


    /**
     * 创建备件采购单
     * 参数
     * {
     * "dataList":[
     * {
     * "ComId", "ComSpeId", "ComSpeMoId", "BuyNumber", "SupId"
     * },
     * "Explain":"XXX"
     * ]
     * }
     *
     * @param params
     * @return
     */
    @PostMapping("/createStandbyOrder")
    public JsonResult createStandbyOrder(@RequestBody Map<String, Object> params) {
        if (!FieldCheckUtil.isMapFieldNotNull(params, "Explain")) {
            return JsonResult.error("参数错误");
        }
        return bms.createStandbyOrder(params);
    }

    /**
     * 撤回备件采购单
     * 功能：
     * 删除备件采购单
     *
     * @return
     */
    @PostMapping("/withdrawStandbyOrder")
    public JsonResult withdrawStandbyOrder(String buyId) {
        if (StringUtils.isBlank(buyId)) {
            return JsonResult.error("参数错误");
        }
        return bms.withdrawStandbyOrder(buyId);
    }

    /**
     * 获取备件采购单  采购中的账单详情
     *
     * @param buyId
     * @return
     */
    @RequestMapping("/getStandbyInfo")
    public JsonResult getStandbyInfo(String buyId) {
        if (StringUtils.isBlank(buyId)) {
            return JsonResult.error("参数错误");
        }
        return bms.getStandbyInfo(buyId);
    }


    /**
     * 进行入库的操作
     * 入参：
     * 采购详情标的ID
     * 入库的数量
     * 采购单主表的编号
     * 型号的编号
     *
     * @param list
     * @return
     */
    @PostMapping("/putStore")
    public JsonResult putStore(@RequestBody List<Map<String, Object>> list) {
        if (list == null || list.isEmpty()) {
            return JsonResult.error("参数为空");
        }
        return bms.putStore(list);
    }
}
